<?php
/**
 * Created by PhpStorm .
 * User: {Administrator}
 * Date: {2022/9/20 18:10}
 * Author: '树叶'
 * Email: 'ayunu@qq.com'
 * Motto Info: '理想如果不向现实做一点点屈服，那么理想也将归于尘土。'
 */

namespace app\api\controller;
use app\api\model\Vehicle;
use app\common\controller\Api;
use app\api\model\User;
use app\common\library\Upload;
use think\Db;
use wxpay\Car;

class Framecar extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';


    /**
     * 车架识别
     */
    public function Ocrcar()
    {
        $uid = $this->auth->id;
        $vin = $this->request->param('vin');
        $car = new Car();
        $response = $car->parse($vin);

        if ($response['code'] == 1)
        {
            $VehicleModel = new Vehicle();
            $data = $VehicleModel->models($uid, $response);
            if ($data)
            {
                $this->success('获取成功', $data, 200);
            }else{
                $this->success('无法识别请手动添加', '', 400);
            }
        }else{
            $this->success('获取失败', '', 400);
        }
    }
    /**
     * 车牌识别
     */
    public function plateSelect()
    {
        $name = $this->request->param('plateNumber');
        if (empty($name))
        {
            $this->success('缺少参数','',400);
        }
        $car  = new Car();
        $data = $car->plateModel($name);

        if (!$data) $this->success('暂无车辆信息','',400);

        $displacement = substr($data['cc'], 0, 2);
        $cc = mb_substr($data['cc'],2,1);
        if ($cc > 4)
        {
            $displacement = $displacement + 1;
        }
        if (empty($data['brand']) || empty($data['brand']) || empty($data['brand']) || empty($data['brand']) || empty($data['brand']))
        {
            $this->success('查询失败,请手动添加','',400);
        }
        $vehicleData = [
            $data['brand'],
            $data['CarModel'],
            $data['ModelYear'],
            $displacement,
            $data['StyleName']
        ];

        $level = 0;
        $vehicleList = [];
        $cp_id = ''; //厂牌
        $cx_id = ''; //车型
        $nf_id = ''; //年份
        $pl = [];
        $pl_id = [];
        $res = true;
        foreach ($vehicleData as $key => $val){
            $level += 1;
            if ($level == 1)
            {
                $cp_id = Vehicle::where('mergename',$val)->value('id');
                if (empty($cp_id))
                {
                    continue;
                    $res = false;
                }
            }elseif ($level == 2){
                $cx_id = Vehicle::where('mergename',$val)->value('id');
                if (empty($cx_id))
                {
                    continue;
                    $res = false;
                }
            }elseif ($level == 3){
                $nf_id = Vehicle::where('mergename',$val)->value('id');
                if (empty($nf_id))
                {
                    continue;
                    $res = false;
                }
            }elseif ($level == 4){
                $pl = Vehicle::where('pid',$nf_id)->select()->column('mergename','id');
            }
        }
        if (empty($res) || empty($pl))
        {
            $this->success('获取失败','',400);
        }
        $vehicleData[3] = $pl;
        $this->success('获取成功',$vehicleData, 200);
    }
    public function model()
    {

        $id = $this->request->param('id');
        if (empty($id))
        {
            $this->success('请选择排量信息','',400);
        }

        $vehicle = new Vehicle();
        $level = $vehicle->where('id',$id)->value('level');
        if ($level != 4)
        {
            $this->success('查询错误,请从新操作','',400);
        }
        $data = $vehicle->where('pid',$id)->where('del',0)->column('mergename');
        if ($data)
        {
            $this->success('获取成功',$data,200);
        }else{
            $this->success('暂无数据','',400);

        }
    }
    /**
     * 车牌添加数据
     */
    public function platedata()
    {
        $params = $this->request->param();
        $uid = $this->auth->id;
        $user = User::get($uid);
        if (empty($user))
        {
            $this->success('请从新登录','',401);
        }

        if (empty($params['brand']) || empty($params['carmodel']) || empty($params['modelyear']) || empty($params['displacement']) || empty($params['stylename']))
        {
            $this->success('缺少参数', '', 400);
        }

        $data = [];
        $level = 0;
        $res = true;
        foreach($params as $key => $val)
        {
            $level += 1;
            if ($level == 1)
            {
                // 厂牌
                $data['label'] = Vehicle::where('mergename',$val)->value('id');
                if (empty($data['label']))
                {
                    $res = false;
                }
            }elseif ($level == 2)
            {
                // 车型
                $data['brand'] = Vehicle::where('pid',$data['label'])->where('mergename',$val)->value('id');
                if (empty($data['brand']))
                {
                    $res = false;
                }
            }elseif ($level == 3)
            { // 年份
                $data['year'] = Vehicle::where('pid',$data['brand'])->where('mergename',$val)->value('id');
                if (empty($data['year']))
                {
                    $res = false;
                }
            }elseif ($level == 4){
                // 排量
                $data['engine'] = Vehicle::where('pid',$data['year'])->where('mergename',$val)->value('id');
                if (empty($data['engine']))
                {
                    $res = false;
                }
            }elseif ($level == 5){
//款型
                $data['models'] = Vehicle::where('pid',$data['engine'])->where('mergename',$val)->value('id');
                if (empty($data['models']))
                {
                    $res = false;
                }
            }else{
                $res = false;
            }
        }

        if (empty($res))
        {
            $this->success('获取失败','',400);
        }
        $data['uid'] = $user['id'];
        $data['name'] = $user['nickname'];
        $response = \app\api\model\UserCar::create($data);
        if ($response)
        {
            $this->success('获取成功',$response,200);
        }else{
            $this->success('添加失败','',400);
        }

    }


    /**
     * 营业执照识别
     * $img_url = 图片地址
     */
    public function yyLicense($img_url = '')
    {

        $result = [
            'msg' => '',
            'data' => '',
            'status' => false
        ];
        $send_file_name = '.'.$img_url;
        $img_post = [
            'appkey' => 'b4ec0c2390a76f114e56f4157e76642d',
            'file'   => new \CURLFile($send_file_name)
        ];
        $data = [
            'code' => '10000',
            'data' => 'a6765ed5c1d54e48aaf69f6e4555c87e',
            'success' => true
        ];
        if ($data['code'] == '10000' && $data['success'])
        {
            $Car = new Car();
            $response = $Car->yyzLicense($data['data']);

            if ($response == true)
            {
                $result['status'] = true;
                $result['msg']    = '营业执照认证成功';
            }else{
                $result['msg']    = '营业执照认证失败';
            }
        }else{
            $result['msg'] = $data['msg'];
        }

        return $result;
    }
}